<HTML>
<SCRIPT  type="text/javascript">
/**
 * Ooganizer TabsMonitor
 *
 * Comments:
 *   This Script adds event handlers to Firefox and keeps track of all the Titles and
 *   URI for each Tab in the browser. This allows Ooganizer to add the correct path
 *   and name into the Workspace.*    
**/
  //file:///C:/Users/HPinvent/Desktop/IterateTabs_TEST.html

function load()
{   
    
    alert("load() called");
    
    // We need elevated Security Permissions (also for our component)
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);                                                  
                                                                 
    var browserWin = wm.getMostRecentWindow("navigator:browser");   
    var tabbrowser = browserWin.getBrowser();


    // try getting the selected tab    
    var strActiveTabTitle = tabbrowser.contentDocument.title;
    var strActiveTabURI = tabbrowser.contentDocument.baseURI;
        
//    var obj = new AddEventHandlersToBrowser;
//    obj();
//    
    //.onfocus = OnFocus; 
}

function unload()
{
    alert("unload() called");
    RemoveEventHandlersFromBrowser();
}
 
// This function is for Test purposes only 
function ButtonClick() 
{
	try 
	{	
	    // *debug* Add Test Tabs
	    // AddSomeTabsForTesting();		   	
		// *debug*
		IterateAllBrowserTabs();	
		   
	} catch (err) {
		alert(err);
		return;
	}		
}

// Window get's focus - IMP
function OnFocus(event)
{
    alert("OnFocus");
}

// Event Tab Added
function TabAdded(event)
{      
  alert("TabAdded");
  //window.IterateAllBrowserTabs();
}
// Event Tab Moved
function TabMoved(event)
{
  //alert("TabMoved");  
  //IterateAllBrowserTabs();
}
// Event Tab Removed
function TabRemoved(event)
{
  //alert("TabRemoved");
  //IterateAllBrowserTabs();
}
// Event Tab Selected
function TabSelected(event)
{
  //alert("TabSelected");
  //IterateAllBrowserTabs();
}

/*handleEvent: function sss_handleEvent(aEvent) 
{
    switch (aEvent.type) 
    {
      case "load":            
        break;
      case "TabOpen":      
      alert("TabOpen123");
      break;
    }
}*/
      
// Add Events here
function AddEventHandlersToBrowser()
{

    alert("AddEventHandlersToBrowser() called");

    // We need elevated Security Permissions (also for our component)
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);                                                  
                                                                 
    var browserWin = wm.getMostRecentWindow("navigator:browser");   
    var tabbrowser = browserWin.getBrowser();
    var container = tabbrowser.tabContainer;
    
    // Add these events to the Browser
    //container.addEventListener("Focus", OnFocus, false);
    container.addEventListener("TabOpen", TabAdded, false);
    //container.addEventListener("TabOpen", this, false);
    container.addEventListener("TabMove", TabMoved, false);
    container.addEventListener("TabClose", TabRemoved, false);
    container.addEventListener("TabSelect", TabSelected, false);
}
// Remove Events here
function RemoveEventHandlersFromBrowser()
{
    // We need elevated Security Permissions (also for our component)
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);                                                  
                                                                 
    var browserWin = wm.getMostRecentWindow("navigator:browser");   
    var tabbrowser = browserWin.getBrowser();      
    var container = tabbrowser.tabContainer;
    
    // remove these events from the browser object
    //container.removeEventListener("OnFocus", OnFocus, false);
    container.removeEventListener("TabOpen", TabAdded, false);
    container.removeEventListener("TabMove", TabMoved, false);
    container.removeEventListener("TabClose", TabRemoved, false);
    container.removeEventListener("TabSelect", TabSelected, false);
}

function AddSomeTabsForTesting()
{
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);
        
    var mainWindow = wm.getMostRecentWindow("navigator:browser");
    
    ////
    // Add Some Sample Tabs
    ////
    mainWindow.getBrowser().addTab("http://google.com");
    mainWindow.getBrowser().addTab("http://yahoo.com");
    mainWindow.getBrowser().addTab("http://myspace.com");
}

function IterateAllBrowserTabs()
{
    // We need elevated Security Permissions (also for our component)
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        
    var strTitles = ""; // this variable will contain all the titles of all the tabs ';' seperated (first title is the selected tab)
    var strURIs = ""; // this variable will contain all the URIs of all the tabs ';' seperated (first URI is the selected tab)

    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);                                                  
                                                                 
    var browserWin = wm.getMostRecentWindow("navigator:browser");   
    var tabbrowser = browserWin.getBrowser();

    // try getting the selected tab    
    var strActiveTabTitle = tabbrowser.contentDocument.title;
    var strActiveTabURI = tabbrowser.contentDocument.baseURI;
    
    // Add the Active Title/URI first
    strTitles += strActiveTabTitle;
    strURIs += strActiveTabURI;    
    
    //*debug*
    //alert(strTitles);
    //alert(strURIs);
    
    var strCurTabTitle;
    var strCurTabURI;
    
    // Check each tab of this browser instance
    var numTabs = tabbrowser.browsers.length;
   
    for(var index=0; index < numTabs; index++) 
    {      
      ////
      // fyi = https://developer.mozilla.org/en/XUL/browser
      ////
      var curBrowser = tabbrowser.getBrowserAtIndex(index);     
      strCurTabTitle = curBrowser.contentTitle;
      strCurTabURI = curBrowser.currentURI.spec;      
            
      // Active Tab is already added, so skip it
      if((strCurTabTitle == strActiveTabTitle) &&
         (strCurTabURI == strActiveTabURI))
      {
          continue;
      }
      else
      {
          // pass out all the titles and URIs
          strTitles += (";" + strCurTabTitle);
          strURIs += (";" + strCurTabURI);
      }        
    }
    
    //*debug*
    alert(strTitles);
    alert(strURIs);         
}
</SCRIPT>
<head>
<title>Testing the Tab Iteration and Event Handling</title>
</head>
<BODY onload="load();" onunload="unload();">
<BUTTON ONCLICK="ButtonClick();" id="go">Go</BUTTON>
<input id="test" value="" />
</BODY>
</HTML>

